package cn.tedu._08weibo.controller;

import cn.tedu._08weibo.base.response.Result;
import cn.tedu._08weibo.base.response.StatusCode;
import cn.tedu._08weibo.mapper.WeiboMapper;
import cn.tedu._08weibo.pojo.dto.WeiboAddParam;
import cn.tedu._08weibo.pojo.entity.Weibo;
import cn.tedu._08weibo.pojo.vo.UserVO;
import cn.tedu._08weibo.pojo.vo.WeiboDetailVO;
import cn.tedu._08weibo.pojo.vo.WeiboIndexVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @author tune_the last
 * @date 2025/11/17 - 18:43
 */
@Slf4j
@Tag(name = "微博模块")
@RestController
@RequestMapping("/v1/weibo")
public class WeiboController {
    @Autowired
    private WeiboMapper weiboMapper;

    @ApiOperationSupport(order = 10)
    @Operation(summary = "发布微博")
    @PostMapping("/insert")
    public Result addWeibo(@RequestBody WeiboAddParam weiboAddParam, HttpSession session) {
        log.debug("WeiboAddParam：" + weiboAddParam);
        //登录校验
        UserVO user = (UserVO) session.getAttribute("user");
        if (user == null){
            return Result.ok(StatusCode.NOT_LOGIN);
        }
        //封装并添加
        Weibo weibo = new Weibo();
        BeanUtils.copyProperties(weiboAddParam, weibo);
        weibo.setUserId(user.getId());
        weibo.setCreateTime(new Date());
        int i = weiboMapper.insertWeibo(weibo);
        if(i > 0){
            return  Result.ok();
        }
        return Result.fail();
    }

    @ApiOperationSupport(order = 20)
    @Operation(summary = "微博首页列表")
    @GetMapping("/selectIndex")
    public Result listWeibo() {
        List<WeiboIndexVO> list = weiboMapper.getAllWeibo();
        return   Result.ok(list);
    }

    @ApiOperationSupport(order = 30)
    @Operation(summary = "微博详情页")
    @GetMapping("/selectById")
    public Result selectById(Long id){
        log.debug("id：" + id);
        WeiboDetailVO weiboDetailVO = weiboMapper.selectById(id);
        return Result.ok(weiboDetailVO);
    }
}

